package com.cq.controller;

import com.cq.pojo.Reply;
import com.cq.pojo.Result;
import com.cq.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 悠久的苍穹
 * @Date: 2021/2/5 19:04
 * @Description: 回复控制层
 * @Version: 1.0
 */
@Controller
public class ReplyController {
    @Autowired
    ReplyService replyService;

    @ResponseBody
    @GetMapping("/getReplies")
    public Result getReplies(@RequestParam int page,
                            @RequestParam int limit) {
        Result result = new Result();
        int count = replyService.getCount();
        List<Reply> replyList = replyService.getReplies(page,limit);
        result.setCode(0);
        result.setCount(count);
        result.setData(replyList);
        result.setMsg("查询成功");
        return result;
    }
    @GetMapping("/getRepliesByDate")
    @ResponseBody
    public Result getRepliesByDate(@RequestParam int page,
                                     @RequestParam int limit,
                                     @RequestParam String start,
                                     @RequestParam String end) {
        Result result = new Result();
        int count = replyService.getReplyCount(start, end);
        List<Reply> replyList = replyService.getRepliesByTime(page, limit, start, end);
        result.setCount(count);
        result.setData(replyList);
        result.setCode(0);
        return result;
    }

    @DeleteMapping("/delReply/{id}")
    @ResponseBody
    public Result delReply(@PathVariable("id") String id) {
        Result result = new Result();
        String[] s = id.split(",");
        for (int i = 0; i < s.length; i++) {
            replyService.deleteReplyById(Integer.parseInt(s[i]));
        }
        result.setCode(200);
        return result;
    }

    @ResponseBody
    @PostMapping("/addReply")
    public Result addReply(@RequestBody Map<String,Object> map) throws Exception {
        Result result = new Result();
        String topic_id = map.get("topic_id").toString();
        String nickname = map.get("nickname").toString();
        String avatarUrl = map.get("avatarUrl").toString();
        String content = map.get("content").toString();
        Reply reply = new Reply();
        reply.setTopic_id(Integer.parseInt(topic_id));
        reply.setNickname(nickname);
        reply.setAvatarUrl(avatarUrl);
        reply.setContent(content);
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        reply.setCreatetime(dateFormat.format(date));
        int i = replyService.addReply(reply);
        if(i==1){
            result.setCode(200);
            result.setMsg("创建成功");
        }else{
            result.setCode(500);
            result.setMsg("创建失败");
        }
        return result;
    }
}
